package com.kyzh.core.uis

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.gushenge.core.base.Base
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.listeners.SimpleResultListener
import com.kyzh.core.R
import kotlinx.android.synthetic.main.webview.view.*
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk27.coroutines.onClick


/*通用弹窗*/
fun Context.alert(title: String?, message: String?, yesButtonText: String?, noButtonText: String?, success: () -> Unit, error: () -> Unit): AlertDialog {
    return customDialog(this, title, message, yesButtonText, noButtonText, success, error)
}

fun Fragment.alert(title: String?, message: String?, yesButtonText: String?, noButtonText: String?, success: () -> Unit, error: () -> Unit): AlertDialog {

    return customDialog(requireContext(), title, message, yesButtonText, noButtonText, success, error)
}

fun Activity.alert(title: String?, message: String?, yesButtonText: String?, noButtonText: String?, success: () -> Unit, error: () -> Unit): AlertDialog {

    return customDialog(this, title, message, yesButtonText, noButtonText, success, error)
}
fun Activity.alert1(title: String?, message: String?, yesButtonText: String?, noButtonText: String?, listener: SimpleResultListener.() -> Unit): AlertDialog {
    val lis = SimpleResultListener()
    lis.listener()
    return customDialog1(this, title, message, yesButtonText, noButtonText, lis)
}
private fun customDialog1(context: Context, title: String?, message: String?, yesButtonText: String?, noButtonText: String?, listener: ResultListener): AlertDialog {
    var dialog: AlertDialog? = null
    dialog = AlertDialog.Builder(context, R.style.BDAlertDialog).setView(
            context.UI {
                verticalLayout {
                    backgroundResource = R.drawable.bg_6dp_white
                    (backgroundDrawable as GradientDrawable).setColor(Color.WHITE)
                    title?.let {
                        textView(it) {
                            textAppearance = R.style.game_detail_tab
                            gravity = Gravity.CENTER
                            textColorResource = R.color.font_33
                            textSize = 14f
                            allCaps = false
                        }.lparams(matchParent, wrapContent) {
                            topMargin = dip(16)
                        }
                    }
                    val webView = webView {

                    }
                    webView .lparams(matchParent, dip(360))
                    val webSettings = webView.settings
                    webSettings.allowFileAccess = true// 设置允许访问文件数据
                    webSettings.setSupportZoom(true)
                    webSettings.builtInZoomControls = true
                    webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
                    webSettings.domStorageEnabled = true
                    webSettings.databaseEnabled = true
                    // 关键性代码，这里要给webview添加这行代码，才可以点击之后正常播放音频。记录一下。
                    webSettings.mediaPlaybackRequiresUserGesture = false
                    webView.webViewClient = WebViewClient()
                    //设置WebView属性，能够执行Javascript脚本
                    webSettings.javaScriptEnabled = true
                    webView.loadUrl(Base.BASEURL + "/?ct=trade&ac=Notice&app=1" ?: "")
                    linearLayout {
                        leftPadding = dip(18)
                        rightPadding = dip(18)
                        noButtonText?.let {
                            button(it) {
                                backgroundResource = R.drawable.bg_6dp_white
                                (backgroundDrawable as GradientDrawable).setColor(Color.WHITE)
                                textColor = Color.rgb(26, 113, 255)
                                textAppearance = R.style.game_detail_tab
                                allCaps = false
                                onClick {
                                    listener.error()
                                    dialog?.dismiss()
                                }
                            }.lparams(dip(0), dip(36)) {
                                weight = 1f
                                margin = dip(12)
                            }
                        }
                        yesButtonText?.let {
                            button(it) {
                                backgroundResource = R.drawable.bg_6dp_white
                                (backgroundDrawable as GradientDrawable).setColor(resources.getColor(R.color.colorPrimaryDark))
                                textColorResource = R.color.white
                                textAppearance = R.style.game_detail_tab
                                allCaps = false
                                onClick {
                                    listener.success()
                                    dialog?.dismiss()
                                }
                            }.lparams(dip(0), dip(36)) {
                                weight = 1f
                                margin = dip(12)
                            }
                        }
                    }.lparams(matchParent, dip(60))


                }


            }.view
    ).create()


    dialog.show()
    return dialog
}

private fun customDialog(context: Context, title: String?, message: String?, yesButtonText: String?, noButtonText: String?, success: () -> Unit, error: () -> Unit): AlertDialog {
    var dialog: AlertDialog? = null
    dialog = AlertDialog.Builder(context, R.style.BDAlertDialog).setView(
            context.UI {
                verticalLayout {
                    backgroundResource = R.drawable.bg_6dp_white
                    (backgroundDrawable as GradientDrawable).setColor(Color.WHITE)
                    title?.let {
                        textView(it) {
                            textAppearance = R.style.game_detail_tab
                            gravity = Gravity.CENTER
                            textColorResource = R.color.font_33
                            textSize = 14f
                            allCaps = false
                        }.lparams(matchParent, wrapContent) {
                            topMargin = dip(16)
                        }
                    }
                    message?.let {
                        textView(it) {
                            textColorResource = R.color.font_33
                            textSize = 12f
                            allCaps = false
                            setLineSpacing(0f, 1.2f)
                        }.lparams(matchParent, wrapContent) {
                            leftMargin = dip(20)
                            rightMargin = dip(20)
                            topMargin = dip(12)
                            bottomMargin = dip(15)
                        }
                    }

                    linearLayout {
                        leftPadding = dip(18)
                        rightPadding = dip(18)
                        noButtonText?.let {
                            button(it) {
                                backgroundResource = R.drawable.bg_6dp_white
                                (backgroundDrawable as GradientDrawable).setColor(Color.WHITE)
                                textColor = ContextCompat.getColor(context, R.color.colorPrimary)
                                textAppearance = R.style.game_detail_tab
                                allCaps = false
                                setOnClickListener {
                                    error()
                                    dialog?.dismiss()
                                }
                            }.lparams(dip(0), dip(36)) {
                                weight = 1f
                                margin = dip(12)
                            }
                        }
                        yesButtonText?.let {
                            button(it) {
                                backgroundResource = R.drawable.bg_6dp_white
                                (backgroundDrawable as GradientDrawable).setColor(resources.getColor(R.color.colorAccent))
                                textColorResource = R.color.white
                                textAppearance = R.style.game_detail_tab
                                allCaps = false
                                setOnClickListener {
                                    success()
                                    dialog?.dismiss()
                                }
                            }.lparams(dip(0), dip(36)) {
                                weight = 1f
                                margin = dip(12)
                            }
                        }
                    }.lparams(matchParent, dip(60))
                }
            }.view
    ).create()
    dialog.show()
    return dialog
}
